package com.pap.rbac.dataclazz.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pap.base.controller.BaseController;
import com.pap.base.util.date.DateUtils;
import com.pap.base.util.jackson.JacksonUtilss;
import com.pap.base.util.string.StringUtilss;
import com.pap.obj.vo.business.DeleteFlagEnum;
import com.pap.obj.vo.business.DisableFlagEnum;
import com.pap.obj.vo.page.Page;
import com.pap.obj.vo.response.ResponseVO;
import com.pap.rbac.agent.dataclazz.IDataClazzAgent;
import com.pap.rbac.agent.dataclazz.dto.DataClazzWithParentDTO;
import com.pap.rbac.agent.datamodule.IDataModuleAgent;
import com.pap.rbac.agent.dataproject.IDataProjectAgent;
import com.pap.rbac.dataclazz.auto.entity.DataClazz;
import com.pap.rbac.datamodule.auto.entity.DataModule;
import com.pap.rbac.dataproject.auto.entity.DataProject;

@RestController
@RequestMapping("/v1/rbac/dataclazz")
public class DataClazzController extends BaseController<DataClazz, IDataClazzAgent> {

	private static Logger logger  =  LoggerFactory.getLogger(DataClazzController.class);
	
	@Resource(name = "dataClazzAgent")
	@Override
	public void setService(IDataClazzAgent agent) {
		super.service = agent;
	}
	
	@Autowired
	private IDataProjectAgent dataProjectAgent;
	
	@Autowired
	private IDataModuleAgent dataModuleAgent;
	
	
	@Override
	@RequestMapping(value = "/add")
	public ResponseVO<DataClazz> add(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "addJSON", required = true) String addJSON) throws Exception {
		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		DataClazz e = objectMapper.readValue(addJSON, DataClazz.class);
		if(StringUtilss.isEmpty(e.getId())) {
			e.setId(UUID.randomUUID().toString());
		}
		e.setDeleteFlag(DeleteFlagEnum.NO.getKey());
		e.setDisableFlag(DisableFlagEnum.NO.getKey());
		e.setCreateIp("0.0.0.0");
		e.setCreateTime(DateUtils.getCurrDateTimeStr());
		e.setCreateUser(loginUserId);
		e.setClientLicenseId(clientLicenseId);

		int operationInt = service.insertSelective(e);
		return ResponseVO.successdata(operationInt);
	}
	
	@RequestMapping(value = "/selectentitybyid")
	public String selectEntityById(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "id", required = true) String id) throws Exception {
		DataClazz e = service.selectByPrimaryKey(id);
		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		return objectMapper.writeValueAsString(e);
	}

	@Override
	@SuppressWarnings("unchecked")
	@RequestMapping("/querypage")
	public ResponseVO<DataClazz> queryPage(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "queryJSON", required = true) String queryJSON,
			@RequestParam(value = "pageJSON", required = true) String pageJSON) throws Exception {

		ObjectMapper mapper = JacksonUtilss.getObjectMapper();
		
		Map<Object, Object> searchMap = new HashMap<Object, Object>();
		
		Map<Object, Object> queryMap = mapper.readValue(queryJSON, Map.class);
		Map<Object, Object> pageMap = mapper.readValue(pageJSON, Map.class);
		
		searchMap.putAll(queryMap);
		
		Page page = null;
		if(pageMap != null && !pageMap.isEmpty() && pageMap.containsKey("page") && pageMap.containsKey("rows")) {
			page = new Page(pageMap.get("page").toString(), pageMap.get("rows").toString(), 0);
			searchMap.put("page", page);
		} else {
			page = new Page("1", "10", 0);
		}
		searchMap.put("clientLicenseId", clientLicenseId);
		
		List<DataClazzWithParentDTO> dataWithParent = new ArrayList<DataClazzWithParentDTO>();
		
		List<DataClazz> operationList =  service.selectListByMap(searchMap);
		if(operationList != null && operationList.size() > 0) {
			for (DataClazz dataModule : operationList) {
				DataClazzWithParentDTO dtoTemp = new DataClazzWithParentDTO();
				BeanUtils.copyProperties(dataModule, dtoTemp);
				DataProject projectTemp = dataProjectAgent.selectByPrimaryKey(dataModule.getProjectId());
				if(projectTemp != null) {
					dtoTemp.setDataProjectName(projectTemp.getName());
				}
				DataModule moduleTemp = dataModuleAgent.selectByPrimaryKey(dataModule.getModuleId());
				if(moduleTemp != null) {
					dtoTemp.setDataModuleName(moduleTemp.getName());
				}
				dataWithParent.add(dtoTemp);
				
				
			}
		}
		
		int count = service.selectCountByMap(searchMap);
		page = new Page(page.getCurrent() + "", page.getLength() + "", count);
		
		return ResponseVO.successdatas(dataWithParent, page);
	}

}
